﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.FTPClient;
using System.Data.OracleClient;
using System.IO;
namespace WPFExcelReport
{
    struct vvPACK
    {
        public string PackNr;
        public SortedList<string, DataRow> WBBSTnr;
    }
    struct vvVKPOS
    {
        public DataRow dwVKPOS;
        public SortedList<string, vvPACK> listVVPACK;
    }
    class DSTAB05DOCReport : BaseReport
    {
        #region ----------------PROPERTIES----------------------------------
        byte[] b;
        public IntexReptServBLClient proxy;
        public OracleConnection OracleDB;        
        string DocType;
        string CellName = "";
        string CellValue = "";
        string strCompany;
        string orderComments = "备注：";
        string strExcelFile;
        public string Customer = "";
        public string Address = "";
        string DateCurrent = "";
        public string PrintType = "JustExcel";
        public List<vvVKPOS> listVKPOS = new List<vvVKPOS>();
        public FTPClient ftpClient;
        #endregion
        public DSTAB05DOCReport()
            : base()
        {

        }
        public DSTAB05DOCReport(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            DocType = dicParamater["DocType"].ToString().Trim();
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
            DateCurrent = DateTime.Now.Year.ToString() + "年" + DateTime.Now.Month.ToString() + "月" + DateTime.Now.Day.ToString() + "日";
            
        }

        public void FillInExcelData(DataSet setSoruce)
        {
            try
            {
                DataTable ABPOS = setSoruce.Tables["ABPOS"];
                foreach (DataRow dataRow in ABPOS.Rows)
                {
                    string FINISHING = dataRow["FINISHING"].ToString().Trim();
                    exAgent.SetCellValue(2, 6, FINISHING);
                    string SO = dataRow["SO"].ToString();
                    exAgent.SetCellValue(3, 2, SO);
                    string ARTICLE = dataRow["ABPART"].ToString().Trim() + '-'
                        + dataRow["ABPFARBE"].ToString().Trim() + '-'
                        + dataRow["ABPVAR"].ToString().Trim();
                    exAgent.SetCellValue(3, 4, ARTICLE);
                    string CUSTOMER = dataRow["CUSTOMER"].ToString().Trim();
                    exAgent.SetCellValue(3, 6, CUSTOMER);
                    string ENDDAT = dataRow["ABPENDDAT"].ToString();
                    exAgent.SetCellValue(4, 4, ENDDAT);
                    exAgent.SetCellValue(4, 6, dataRow["QTY"].ToString());
                    string COUNT = dataRow["COUNT"].ToString();
                    exAgent.SetCellValue(7, 1, COUNT);
                    string GWID = dataRow["GWID"].ToString();
                    exAgent.SetCellValue(7, 4, GWID);
                    string GDDIK = dataRow["GDDIK"].ToString();
                    exAgent.SetCellValue(7, 3, GDDIK);
                    string FDDIK = dataRow["FDDIK"].ToString();
                    exAgent.SetCellValue(7, 5, FDDIK);
                    string FWID = dataRow["FWID"].ToString();
                    exAgent.SetCellValue(7, 6, FWID);
                    string WORKFLOW = dataRow["WORKFLOW"].ToString().Trim();
                    exAgent.SetCellValue(8, 2, WORKFLOW);
                    string USER = dataRow["ABPNEUBEN"].ToString().Trim();
                    exAgent.SetCellValue(35, 4, USER);
                    string remark = dataRow["REMARK"].ToString().Trim();
                    exAgent.SetCellValue(24, 2, remark);

                }
                string DAT = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
                exAgent.SetCellValue(35, 6, DAT);
                DataTable WBBST = setSoruce.Tables["WBBST"];
                foreach (DataRow dataRow in WBBST.Rows)
                {
                   string ORDERNO = dataRow["WBBAUFKRS"].ToString()+'-'
                       + System.Convert.ToInt32(dataRow["WBBAUFLFD"].ToString())+'-'
                       + System.Convert.ToInt16(dataRow["WBBAUFPOS"].ToString())+'.'
                       + System.Convert.ToInt16(dataRow["WBBTP"].ToString());
                   exAgent.SetCellValue(2,2 , ORDERNO);
                   string STARTDAT = dataRow["STARTDAT"].ToString();
                   exAgent.SetCellValue(4, 2, STARTDAT);
                   string QTY = dataRow["PENUM"].ToString() + "段"+
                       dataRow["QTY"].ToString() + "米";
                   exAgent.SetCellValue(5, 2, QTY);
                   
                }
                DataTable ABFOLGE = setSoruce.Tables["ABFOLGE"];
                int x = 10;
                foreach (DataRow dataRow in ABFOLGE.Rows)
                {
                    string ABFBZAGANG = dataRow["ABFBZAGANG"].ToString().Trim();
                    exAgent.SetCellValue(x, 1, ABFBZAGANG);
                    x++;
                } 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        
        public override void WriteExcelReport(string fileType)
        {

            string filename = "";
            string sheetName = "";
            string Standard = "AB05DOC";
            DataRow dwWBBST = null;
            if (dsSource.Tables.Contains("WBBST"))
            {
                if (dsSource.Tables["WBBST"].Rows.Count > 0)
                    dwWBBST = dsSource.Tables["WBBST"].Rows[0];
            }
            if (dwWBBST == null)
                return;
            string KRS, LFD, POS, LOT;
            KRS = dwWBBST["WBBAUFKRS"].ToString();
            LFD = dwWBBST["WBBAUFLFD"].ToString();
            POS = dwWBBST["WBBAUFPOS"].ToString();
            LOT = dwWBBST["WBBTP"].ToString();

            strExcelFile = @"DST\DSTInventory.xls";
            string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\DST\DSTInventory.xls");
            exAgent.Open(filePath);
            exAgent.Company = strCompany;
            exAgent.strExcelFile = strExcelFile;
            //string filedate = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
            filename = @"Production\" + KRS + LFD + POS + ".xls";
            //sheetName = DateTime.Now.ToShortTimeString().Replace(":", "_").Replace(" ", "_").Replace("/", "_");
            sheetName = LOT;
            exAgent.CreateNewSheet(filename, Standard, sheetName);
            FillInExcelData(dsSource);
            base.WriteExcelReport(fileType);
            //exAgent.Save();
            //exAgent.Close();
            string files = Path.Combine(PathHelper.ApplicationPath, @"Report\"+filename);
            //System.Diagnostics.Process.Start("EXCEL.EXE", files);                
            
        }

    }
}
